package com.redwood.report.widget;

        import android.animation.ValueAnimator;
        import android.annotation.TargetApi;
        import android.content.Context;
        import android.content.res.TypedArray;
        import android.graphics.Canvas;
        import android.graphics.Color;
        import android.graphics.Paint;
        import android.graphics.RectF;
        import android.graphics.Typeface;
        import android.os.Build;
        import android.util.AttributeSet;
        import android.view.View;
        import android.view.animation.AccelerateInterpolator;
        import android.view.animation.OvershootInterpolator;

        import com.redwood.report.R;
        import com.redwood.report.util.UIUtil;
        import com.redwood.report.util.Utils;

/**
 *
 * --------自定义控件-------
 */
public class RingCustomView extends View {


    //画笔
    private Paint paint;
    private RectF oval;


    //圆弧颜色
    private int roundColor;
    //进度颜色
    private int progressColor;
    //最大进度
    private int max = 1000;
    //当前进度
    private int progress = 300;
    //圆弧宽度
    private int roundWidth = 3;

    private int viewWidth; //宽度--控件所占区域

    private float nowPro = 0;//用于动画

    private ValueAnimator animator;

    public RingCustomView(Context context) {
        super(context);
    }

    public RingCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs, context);
    }

    public RingCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs, context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public RingCustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAttrs(attrs, context);
    }


    private void initAttrs(AttributeSet attr, Context context) {
        TypedArray array = context.obtainStyledAttributes(attr, R.styleable.CustomView);


        roundColor = array.getColor(R.styleable.CustomView_roundColor, context.getResources().getColor(R.color.gray));//环形颜色
        progressColor = array.getColor(R.styleable.CustomView_progressColor, context.getResources().getColor(R.color.report_blue));//进度颜色
        roundWidth = UIUtil.dp2px(context,array.getInt(R.styleable.CustomView_roundWidth, 3));//圆环宽度
        progress=array.getInt(R.styleable.CustomView_progressNow,500);

        array.recycle();



    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        viewWidth = w;//得到宽度以此来计算控件所占实际大小

        //计算画布所占区域
        oval = new RectF();
        oval.left = roundWidth + getPaddingLeft();
        oval.top = roundWidth + getPaddingTop();
        oval.right = viewWidth - roundWidth - getPaddingRight();
        oval.bottom = viewWidth - roundWidth - getPaddingBottom();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setAntiAlias(true);                        //设置画笔为无锯齿
        paint.setColor(roundColor);                     //设置画笔颜色
        paint.setStrokeWidth(roundWidth);                //线宽
        paint.setStrokeCap( Paint.Cap.ROUND );
        paint.setStyle(Paint.Style.STROKE);              //空心
        canvas.drawArc(oval, 130, 280, false, paint);    //绘制圆弧

        //画进度层
        paint.setColor(progressColor);
        paint.setStrokeWidth(roundWidth + 1);
        canvas.drawArc(oval, 130, 280 * nowPro / max, false, paint);  //绘制圆弧
    }


    private int getDefaultHeight() {
        return 0;
    }

    private int getDefaultWidth() {
        return 0;
    }


    public int getRoundColor() {
        return roundColor;
    }

    public void setRoundColor(int roundColor) {
        this.roundColor = roundColor;
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        //动画
        animator = ValueAnimator.ofFloat(0, progress);
        animator.setDuration(1800);
        animator.setInterpolator(new AccelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                nowPro = (float) animation.getAnimatedValue();
                postInvalidate();
            }
        });
        animator.start();
    }
}